﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using EkStudio.com.bean.xml.BillChart;
using EkStudio.com.bean.xml.reflection.JAXB;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.common.clipboard;
using EkStudio.com.common.unit;
using EkStudio.com.ui.form;
using EkStudio.com.ui.unitbill.property;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace EkStudio.com.ui.unitbill
{
    public partial class BillChartUnitPanel :XtraUserControl, ProjectUnitBillInterface
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillChartUnitPanel));


        private bool readOnly = false;
        private bool save = true;
        private ProjectRootPanel RootPanel = null;
        private ProjectUnitBillInterface ParentUnit = null;
        private List<ProjectUnitBillInterface> SubjectUnit = new List<ProjectUnitBillInterface>();//子面板

        private BillChartModuleItem Chart = null;

        private BillChartModuleToProperty modProperty = null;

        private ModuleUiFieldProperty fieldUiProperty = null;

        private ModuleUiJScriptProperty JScriptPropery = null;

        public BillChartUnitPanel(ProjectRootPanel root,BillChartModuleItem bean,bool read)
        {
            InitializeComponent();
            RootPanel = root;
            readOnly = read;
            Chart = bean;
            InitUnitPanel();
        }

        private void InitUnitPanel()
        {
            //初始目录结构
            if (RootPanel.MenuItem.Business != null && RootPanel.MenuItem.Business.Modules != null
                && RootPanel.MenuItem.Business.Modules.Count > 0)
            {
                RepositoryItemMenuEdit.Items.Clear();
                for (int i = 0; i < RootPanel.MenuItem.Business.Modules.Count; i++)
                {
                    CheckedListBoxItem item = new CheckedListBoxItem(RootPanel.MenuItem.Business.Modules[i].Caption, RootPanel.MenuItem.Business.Modules[i].Caption);
                    RepositoryItemMenuEdit.Items.Add(item);
                }
            }

            //实例化模块属性列表
            modProperty = new BillChartModuleToProperty(Chart);
            BillPropertyGrid.SelectedObject = modProperty;
            RepositoryItemChartViewList.Items.AddRange(BeanUtil.CHARTTYPE);


            //实例化字段属性面板
            fieldUiProperty = new ModuleUiFieldProperty(Chart, RootPanel, this);
            fieldUiProperty.Dock = DockStyle.Fill;
            fieldUiProperty.CaptionChanged += DataFieldCaptionChanged;//注册标签修改事件

            JScriptPropery = new ModuleUiJScriptProperty(Chart, RootPanel, this);
            JScriptPropery.CaptionChanged += JScriptPropery_CaptionChanged;
            JScriptPropery.Dock = DockStyle.Fill;



            FieldPanelPropty.Controls.Add(fieldUiProperty);
            FieldPanelPropty.Controls.Add(JScriptPropery);
            FieldPanelPropty.Visible = false;


            //初始化字段
            if (Chart.FieldProperties == null) Chart.FieldProperties = new BillChartModuleFieldsRoot();
            if (Chart.FieldProperties.Fields == null) Chart.FieldProperties.Fields = new List<BillUiFieldsItem>();
            if (Chart.FieldProperties.Fields.Count > 0)
            {
                foreach(BillUiFieldsItem field in Chart.FieldProperties.Fields)
                {
                    ListViewItem it = new ListViewItem(field.Caption, 0);
                    it.Name = field.Key;
                    it.Tag = field;
                    DataFieldView.Items.Add(it);
                }
            }

            //初始化脚本字段
            if (Chart.ExpandCode == null) Chart.ExpandCode = new BillExpandCodeRoot();
            if (Chart.ExpandCode.Jscript == null) Chart.ExpandCode.Jscript = new BillExpandJsCodeRoot();
            if (Chart.ExpandCode.Jscript.Item == null) Chart.ExpandCode.Jscript.Item = new List<BillExpandJsCodeItem>();
            if(Chart.ExpandCode.Jscript.Item.Count > 0)
            {
                for (int i = 0; i < Chart.ExpandCode.Jscript.Item.Count; i++)
                {
                    BillExpandJsCodeItem JItem = Chart.ExpandCode.Jscript.Item[i];
                    ListViewItem ViewIt = new ListViewItem(JItem.Caption, 2);
                    ViewIt.Name = JItem.Key;
                    ViewIt.Tag = JItem;
                    ScriptListView.Items.Add(ViewIt);
                }
            }


            row_bndoe_seven.Properties.ReadOnly = ReadOnly;
            row_bnode_five.Properties.ReadOnly  = ReadOnly;
            row_bnode_four.Properties.ReadOnly  = ReadOnly;
            row_bnode_six.Properties.ReadOnly   = ReadOnly;
            row_bnode_three.Properties.ReadOnly = ReadOnly;
            row_bnode_slist.Properties.ReadOnly = ReadOnly;


        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="clazz">复制后的clazz</param>
        /// <returns></returns>
        public bool Copy(out object clazz)
        {
            bool val = false;
            clazz = null;

            return val;
        }

        private void JScriptPropery_CaptionChanged(object sender, PropertyDataEvent e)
        {
            if (ScriptListView.SelectedItems.Count == 1)
            {
                ScriptListView.SelectedItems[0].Text = e.Value.ToString();
            }
        }

        #region 接口实现
        public string BillKey
        {
            get
            {
                return Chart.getKey();
            }
        }

        public bool isSubUnitBill
        {
            get
            {
                return false;
            }
        }

        public ProjectUnitBillInterface ParentPage
        {
            get { return ParentUnit;}
            set { ParentUnit = value; }
        }

        public bool ReadOnly
        {
            get
            {
                return readOnly;
            }

            set
            {
                readOnly = value;
            }
        }

        public bool SaveStatus
        {
            get { return save; }
            set { save = value; RootPanel.SaveStatus = value; }
        }

        public List<ProjectUnitBillInterface> SubjectPage
        {
            get { return SubjectUnit; }
        }

        public void AddSubjectBill(ProjectUnitBillInterface bill)
        {
            if (!SubjectUnit.Contains(bill))
            {
                bill.ParentPage = this;
                SubjectUnit.Add(bill);
            }
        }

        /// <summary>
        /// 子页面保存状态
        /// </summary>
        /// <returns></returns>
        private bool isSubSaveStatus()
        {
            bool result = true;
            if (SubjectUnit.Count > 0)
            {
                for (int i = 0; i < SubjectUnit.Count; i++)
                {
                    if (!SubjectUnit[i].SaveStatus)
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }

        public bool close()
        {
            bool result = true;
            if (!save || !this.isSubSaveStatus())
            {
                DialogResult dialog = XtraMessageBox.Show("当前数据已更改是否保存?", "提示", MessageBoxButtons.YesNoCancel);
                if (dialog == DialogResult.Yes)
                {
                    this.saveData(true);
                    result = true;
                }
                else if (dialog == DialogResult.No)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }

            if (result)
            {
                //释放模块占用
                try
                {
                    RootPanel.Http.ReleaseModule(Chart.getKey(), Chart.getCaption(), ProjectUtil.TYPE_BILL_CHARTBAR);
                }
                catch (Exception e)
                {
                    this.RootPanel.AddConsoleText("页面关闭失败", true);
                    logger.Error("页面关闭失败", e);
                }

            }

            return result;
        }

        public bool ContainSubject(ProjectUnitBillInterface bill)
        {
            return SubjectUnit.Contains(bill);
        }

        public void RemoveSubjectBill(ProjectUnitBillInterface bill)
        {
            if (ContainSubject(bill))
            {
                bill.ParentPage = null;
                SubjectUnit.Remove(bill);
            }
        }

        public void saveData(bool updateParent)
        {
            if (!readOnly && (!save || !isSubSaveStatus()))
            {
                if (!isSubSaveStatus())
                {
                    //先保存子表
                    foreach (ProjectUnitBillInterface subPage in SubjectUnit)
                    {
                        subPage.saveData(false);
                    }
                }
                Chart.ChartProperties.RootVersion = BeanUtil.VERSION;
                Chart.ChartProperties.Owner.LastMender = RootPanel.UserInfo.UserName;
                Chart.ChartProperties.Owner.LastModified = StringUtility.getZwStandLongDate();

                Chart.autoInitLanguage(RootPanel.ProjectVersion);
                Chart.autoInitQueryCode();

                JAXBContext<BillChartModuleItem> jaxb = new JAXBContext<BillChartModuleItem>();

                try
                {
                    string xml = jaxb.marshal(Chart, true);
                    logger.Info(xml);
                    string result = RootPanel.Http.SaveXmlBean(Chart.getKey(), ProjectUtil.TYPE_BILL_CHARTBAR, xml);
                    XmlDocument docXml = new XmlDocument();

                    docXml.LoadXml(result);
                    XmlElement root = docXml.DocumentElement;
                    XmlNode node = root.SelectSingleNode("result");
                    if (node.InnerText == "0")
                    {
                        RootPanel.AddConsoleText(string.Format("保存模块[{0}]成功", Chart.getCaption()), false);
                    }
                    else
                    {
                        RootPanel.AddConsoleText(string.Format("保存模块[{0}]失败", Chart.getCaption()), false);
                    }
                }
                catch (Exception e)
                {
                    RootPanel.AddConsoleText(string.Format("保存模块[{0}]失败,请检查网络连接", Chart.getCaption()), false);
                    logger.Error("保存失败", e);
                }
                save = true;
                if (updateParent) RootPanel.SaveStatus = true;
            }
        }

        #endregion

        /// <summary>
        /// 属性框-数据源按钮事件-目的弹出数据源编辑框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryItemSQLEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ChartDataSourceForm form = new ChartDataSourceForm(Chart,RootPanel,this);
            form.ShowDialog();
        }
        /// <summary>
        /// 属性框-扩展代码按钮事件-打开扩展代码编辑功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryItemJavaEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (Chart.ExpandCode == null) Chart.ExpandCode = new BillExpandCodeRoot();
            if (Chart.ExpandCode.JavaPlugin == null) Chart.ExpandCode.JavaPlugin = new BillExpandJavaCodeRoot();
            if (Chart.ExpandCode.JavaPlugin.JavaItem == null) Chart.ExpandCode.JavaPlugin.JavaItem = new List<BillExpandJavaCodeItem>();
            if(e.Button.Kind == ButtonPredefines.Delete)
            {
                if(Chart.ExpandCode.JavaPlugin.JavaItem.Count>0 && XtraMessageBox.Show("是否删除扩展代码", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Chart.ExpandCode.JavaPlugin.JavaItem.Clear();
                    SaveStatus = false;
                    BillPropertyGrid.Refresh();
                }
            }
            else
            {
                BillExpandJavaCodeItem java = null;
                if (Chart.ExpandCode.JavaPlugin.JavaItem != null && Chart.ExpandCode.JavaPlugin.JavaItem.Count == 1)
                {
                    java = Chart.ExpandCode.JavaPlugin.JavaItem[0];
                }
                else
                {
                    java = new BillExpandJavaCodeItem();

                    java.Key = string.Format("ChartPlugin_{0}_Clazz", Chart.getKey());
                    java.Caption = "扩展代码";
                    java.JavaPackage = string.Format("com.kootop.plugin.{0}", Chart.getJavaPackage());
                    java.Content = BeanUtil.getJavaCode_Chart(java.Key, java.JavaPackage);
                    Chart.ExpandCode.JavaPlugin.addNode(java, Chart);
                    SaveStatus = false;
                    BillPropertyGrid.Refresh();
                }
                ProjectUnitBillInterface subject = RootPanel.OpenJavaPanel(this, Chart, java);
                AddSubjectBill(subject);
            }
        }

        /// <summary>
        /// 脚本事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryItemScriptBtn_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (Chart.ExpandCode == null) Chart.ExpandCode = new BillExpandCodeRoot();
            if (Chart.ExpandCode.Jscript == null) Chart.ExpandCode.Jscript = new BillExpandJsCodeRoot();
            if (Chart.ExpandCode.Jscript.Item == null) Chart.ExpandCode.Jscript.Item = new List<BillExpandJsCodeItem>();
            if (e.Button.Kind == ButtonPredefines.Delete)//删除脚本
            {
                if(Chart.ExpandCode.Jscript.Item.Count >0 && XtraMessageBox.Show("是否删除脚本","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Chart.ExpandCode.Jscript.Item.Clear();
                    Chart.ChartProperties.Advanced.Javascript = "";
                    SaveStatus = false;
                    BillPropertyGrid.Refresh();
                }
            }
            else
            {
                BillExpandJsCodeItem jscript = null;
                if(Chart.ExpandCode.Jscript.Item.Count == 1)
                {
                    jscript = Chart.ExpandCode.Jscript.Item[0];
                }
                else
                {
                    jscript = new BillExpandJsCodeItem();
                    jscript.Key = "jtableOndbClick";
                    jscript.Caption = "行双击事件";
                    jscript.PluginType = "selectForm";
                    System.Text.StringBuilder str = new System.Text.StringBuilder();
                    str.Append("function jtableOndbClick(){").AppendLine();
                    str.Append("    return true;").AppendLine();
                    str.Append("}").AppendLine();
                    jscript.Content = str.ToString();
                    Chart.ChartProperties.Advanced.Javascript = "jtableOndbClick";
                    Chart.ExpandCode.Jscript.addNode(jscript,Chart);
                    SaveStatus = false;
                    BillPropertyGrid.Refresh();
                }
                ProjectUnitBillInterface subject = RootPanel.OpenJScriptPanel(this, Chart, jscript);
                AddSubjectBill(subject);
            }
        }

        private void DataFieldView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (DataFieldView.SelectedItems.Count > 0)
            {
                if(DataFieldView.SelectedItems.Count == 1)
                {
                    ListViewItem it = DataFieldView.SelectedItems[0];
                    if (it.Tag is BillUiFieldsItem)
                    {
                        BillUiFieldToProperty toBean = new BillUiFieldToProperty((BillUiFieldsItem)it.Tag, null);
                        fieldUiProperty.SelectField = toBean;
                        fieldUiProperty.Visible = true;
                        JScriptPropery.Visible = false;
                        FieldPanelPropty.Visible = true;
                    }
                }
                else
                {
                    BillUiFieldToProperty[] groups = new BillUiFieldToProperty[DataFieldView.SelectedItems.Count];
                    for (int i = 0; i < DataFieldView.SelectedItems.Count; i++)
                    {
                        ListViewItem vIt = DataFieldView.SelectedItems[i];
                        object tag = vIt.Tag;
                        groups[i] = new BillUiFieldToProperty((BillUiFieldsItem)tag, null);
                    }
                    fieldUiProperty.SelectFields = groups;
                }
                
            }
            else
            {
                FieldPanelPropty.Visible = false;
            }
        }
        /// <summary>
        /// 脚本选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScriptListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (ScriptListView.SelectedItems.Count > 0)
            {
                object temp = ScriptListView.SelectedItems[0].Tag;
                if (temp is BillExpandJsCodeItem)
                {
                    BillExpandJsCodeItem code = (BillExpandJsCodeItem)temp;
                    JScriptPropery.SelectField = new BillUiJscriptToProperty(code);
                }

                fieldUiProperty.Visible = false;
                JScriptPropery.Visible = true;
                FieldPanelPropty.Visible = true;
            }
            else
            {
                FieldPanelPropty.Visible = false;
            }
        }

        private bool viewClick = false;
        private bool moveing = false;

        /// <summary>
        /// 标签属性修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataFieldCaptionChanged(object sender,PropertyDataEvent e)
        {
            if (DataFieldView.SelectedItems.Count  == 1)
            {
                DataFieldView.SelectedItems[0].Text = e.Value.ToString();
            }
        }

        /// <summary>
        /// 新建字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopNewNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;

            if (FieldTabControl.SelectedTabPageIndex == 0)
            {
                BillUiFieldsItem it = new BillUiFieldsItem();
                it.DataProperty = new BillUiFieldsDataProperty();
                it.EditBorder = new BillUiFieldsEditBorderProperty();
                it.EditProperty = new BillUiFieldsEditProperty();
                it.FormulaProperty = new BillUiFieldsFormulaProperty();
                it.ViewBorder = new BillUiFieldsViewBorderProperty();
                Chart.FieldProperties.addNode(it, Chart);
                ListViewItem view = new ListViewItem(it.Caption, 0);
                view.Tag = it;
                DataFieldView.Items.Add(view);
                view.Selected = true;
                SaveStatus = false;
            }
            else if(FieldTabControl.SelectedTabPageIndex == 1)
            {
                BillExpandJsCodeItem js = new BillExpandJsCodeItem();

                Chart.ExpandCode.Jscript.addNode(js, Chart);
                ListViewItem it = new ListViewItem(js.Caption, 2);
                it.Tag = js;
                ScriptListView.Items.Add(it);
                it.Selected = true;
                SaveStatus = false;
            }

            
        }

        private void FieldPopDeleteNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {
                if (DataFieldView.SelectedItems.Count == 1)
                {
                    DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中字段吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DR == DialogResult.Yes)
                    {
                        ListViewItem view = DataFieldView.SelectedItems[0];
                        if (view.Tag is BillUiFieldsItem)
                        {
                            BillUiFieldsItem field = view.Tag as BillUiFieldsItem;
                            Chart.FieldProperties.removeNode(field);
                            view.Tag = null;
                            field = null;
                        }
                        DataFieldView.Items.Remove(view);
                        FieldPanelPropty.Visible = false;
                        view = null;
                        SaveStatus = false;
                    }
                    
                }
            }
            else if(FieldTabControl.SelectedTabPageIndex == 1)
            {
                if(ScriptListView.SelectedItems.Count == 1)
                {//删除脚本
                    DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中脚本吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (DR == DialogResult.Yes)
                    {
                        ListViewItem it = ScriptListView.SelectedItems[0];
                        BillExpandJsCodeItem js = it.Tag as BillExpandJsCodeItem;
                        Chart.ExpandCode.Jscript.removeNode(js);
                        it.Tag = null;
                        ScriptListView.Items.Remove(it);
                        js = null;
                        it = null;
                        SaveStatus = false;
                    }
                }
            }
            
        }
        /// <summary>
        /// 字段复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopCopyNode_Click(object sender, EventArgs e)
        {
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {
                if(DataFieldView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillUiFieldsItem[] fields = new BillUiFieldsItem[DataFieldView.SelectedItems.Count];
                    int i = 0;
                    foreach (ListViewItem it in DataFieldView.SelectedItems)
                    {
                        try
                        {
                            BillUiFieldsItem Cp = (BillUiFieldsItem)((BillUiFieldsItem)it.Tag).Clone();
                            fields[i] = Cp;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("字段不支持复制", ee);
                            RootPanel.AddConsoleText("字段不支持复制", true);
                            sccuess = false;
                        }
                        i++;
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fields);
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPageIndex == 1)
            {
                if (ScriptListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillExpandJsCodeItem[] fills = new BillExpandJsCodeItem[ScriptListView.SelectedItems.Count];
                    for (int i = 0; i < ScriptListView.SelectedItems.Count; i++)
                    {
                        BillExpandJsCodeItem fill = ScriptListView.SelectedItems[i].Tag as BillExpandJsCodeItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillExpandJsCodeItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("脚本不支持复制", ee);
                            RootPanel.AddConsoleText("脚本不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                    }
                }
            }
        }
        /// <summary>
        /// 粘贴功能实现
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopPasteNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if (!JClipboard.ContainData()) return;
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {
                if (JClipboard.Paste()[0] is BillUiFieldsItem)
                {
                    foreach (BillUiFieldsItem field in JClipboard.Paste())
                    {
                        Chart.FieldProperties.addNode(field, Chart);
                        ListViewItem it = new ListViewItem(field.Caption, 0);
                        it.Tag = field;
                        DataFieldView.Items.Add(it);
                    }
                    SaveStatus = false;
                    JClipboard.Clear();
                }
            }
            else if(FieldTabControl.SelectedTabPageIndex == 1)
            {
                if (JClipboard.Paste()[0] is BillExpandJsCodeItem)
                {
                    foreach (BillExpandJsCodeItem fill in JClipboard.Paste())
                    {
                        //Module.DataFill.addNode(fill, Module);
                        Chart.ExpandCode.Jscript.addNode(fill, Chart);
                        ListViewItem it = new ListViewItem(fill.Caption, 2);
                        it.Tag = fill;
                        ScriptListView.Items.Add(it);
                    }
                    SaveStatus = false;
                    JClipboard.Clear();
                }
            }
            
        }

        private void FieldPopCutNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if (DataFieldView.SelectedItems.Count > 0)
            {
                List<BillUiFieldsItem> cpList = new List<BillUiFieldsItem>();
                foreach (ListViewItem it in DataFieldView.SelectedItems)
                {
                    if (it.Tag is BillUiFieldsItem)
                    {
                        BillUiFieldsItem field = it.Tag as BillUiFieldsItem;
                        BillUiFieldsItem clone = field.Clone() as BillUiFieldsItem;
                        if (clone != null)
                        {
                            cpList.Add(clone);
                        }
                    }
                }
                if (cpList.Count > 0)
                {
                    JClipboard.copy(cpList.ToArray());
                }
                cpList.Clear();
                cpList = null;

                foreach (ListViewItem it in DataFieldView.SelectedItems)
                {
                    BillUiFieldsItem field = it.Tag as BillUiFieldsItem;
                    if (field != null) Chart.FieldProperties.removeNode(field);
                    it.Tag = null;
                    field = null;
                    DataFieldView.Items.Remove(it);
                }
                SaveStatus = false;
                FieldPanelPropty.Visible = false;

            }
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopCheckAllNode_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;  
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {

            } 
        }

        private void FieldPopUpNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {
                moveing = true;

                if (DataFieldView.SelectedItems.Count != 1) return;
                ListViewItem view = DataFieldView.SelectedItems[0];
                BillUiFieldsItem field = view.Tag as BillUiFieldsItem;
                int index = DataFieldView.Items.IndexOf(view);
                if (index > 0)
                {
                    Chart.FieldProperties.moveUp(field);
                    ListViewItem PevView = DataFieldView.Items[index - 1];
                    PevView.Tag = Chart.FieldProperties.Fields[index - 1];
                    PevView.Text = (PevView.Tag as BillUiFieldsItem).Caption;
                    view.Tag = Chart.FieldProperties.Fields[index];
                    view.Text = (view.Tag as BillUiFieldsItem).Caption;
                    PevView.Selected = true;
                    view.Selected = false;
                    SaveStatus = false;
                }
                moveing = false;
            }
            
        }
        /// <summary>
        /// 字段下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopDownNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if(FieldTabControl.SelectedTabPageIndex == 0)
            {
                moveing = true;

                ListViewItem view = DataFieldView.SelectedItems[0];
                BillUiFieldsItem field = view.Tag as BillUiFieldsItem;
                int index = DataFieldView.Items.IndexOf(view);
                if (index < DataFieldView.Items.Count - 1)
                {
                    Chart.FieldProperties.moveDown(field);
                    ListViewItem LastView = DataFieldView.Items[index + 1];
                    LastView.Tag = Chart.FieldProperties.Fields[index + 1];
                    LastView.Text = (LastView.Tag as BillUiFieldsItem).Caption;
                    view.Tag = Chart.FieldProperties.Fields[index];
                    view.Text = (view.Tag as BillUiFieldsItem).Caption;
                    LastView.Selected = true;
                    view.Selected = false;
                    SaveStatus = false;
                }

                moveing = false;

            }
            
        }

        /// <summary>
        /// 单据属性值改变触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BillPropertyGrid_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            SaveStatus = false;
            if(e.Row.Properties.FieldName == "Content")
            {
                if (!e.Value.Equals("[JavaExp]"))
                {
                    Chart.ExpandCode.JavaPlugin.JavaItem.Clear();
                    BillPropertyGrid.Refresh();
                }
            }
            else if(e.Row.Properties.FieldName == "ChartView")
            {
                if (e.Value.Equals(BeanUtil.CHART_TYPE_ST_DATA))
                {
                    row_bndoe_seven.Visible = true;
                }
                else
                {
                    row_bndoe_seven.Visible = false;
                }
                BillPropertyGrid.Refresh(); 
            }
            else if(e.Row.Properties.FieldName == "TreeEnable")
            {
                if (e.Value.Equals(true))
                {
                    row_bnode_eight.Visible = true;
                    row_bnode_night.Visible = true;
                }
                else
                {
                    row_bnode_eight.Visible = false;
                    row_bnode_night.Visible = false;
                }
                BillPropertyGrid.Refresh();
            }
        }

        private void ScriptListView_Click(object sender, EventArgs e)
        {

        }

        
    }
}
